<?php
class ChannelProduct extends KF_Auth_Controller{
	/*
        药店列表
    */
    public function index()
    {   
        if(IS_POST)
        {
            $where = array();
            $config = array(
                'table' => 'okdkyao.dk_app_yaodian',
                'columns' => array(
                    array('db' => 'ID'),
                    array('db' => 'Caption')
                ),
                'where' => implode(' and ', $where)
            );
            $this->load->library('dataTables', $config);
            $result = $this->datatables->search();
            
            exit(json_encode($result));
        }
        
        $this->load->view("channel_product/index",$res);
    }

    /*
        渠道列表
    */
    public function channelList()
    {
        if(IS_POST){
            $this->load->model("Common_model", "CommonModel");
            $CommonModel = $this->CommonModel;

            $channel = $CommonModel->getAllChannelInfo();

            $res = array(
                "status" => "success",
                "data" => array_values($channel)
            );

            header('Content-type: application/json');
            echo json_encode($res);
        }else{
            $this->load->view("channel_product/channel_list", array());
        }
    }

    /*
        商品列表
    */
    public function productList()
    {
        $chn_id = $_GET['chn_id'];
        $store_id = $_GET['store_id'];

        if(IS_POST){
            $this->load->model("ChannelProduct_model", "ChannelProductModel");
            $ChannelProductModel = $this->ChannelProductModel;

            $productList = $ChannelProductModel->getProductList($chn_id, $store_id);

            $res = array(
                "status" => "success",
                "data" => $productList
            );
            header('Content-type: application/json');
            echo json_encode($res);
        }else{
            $this->load->model("Common_model", "CommonModel");
            $CommonModel = $this->CommonModel;
            $channel = $CommonModel->getAllChannelInfo($chn_id);

            $data = array(
                "chn_name" => $channel['name']
            );
            $this->load->view("channel_product/product_list", $data);
        }
    }

    /*
        同步商品列表
    */
    public function syncProduct()
    {
        $chn_id = $_GET['chn_id'];
        $store_id = $_GET['store_id'];
        $this->load->model("ChannelProduct_model", "ChannelProductModel");
        $ChannelProductModel = $this->ChannelProductModel;
        if(IS_POST){
            $syncProduct = $ChannelProductModel->getSyncProductList($chn_id, $store_id);

            $res = array(
                "status" => "success",
                "data" => $syncProduct
            );

            header('Content-type: application/json');
            echo json_encode($res);
        }else{
            $this->load->model("Common_model", "CommonModel");
            $CommonModel = $this->CommonModel;
            $channel = $CommonModel->getAllChannelInfo($chn_id);
            $syncCnt = $ChannelProductModel->getSyncQueueCnt($chn_id);

            $data = array(
                "chn_name" => $channel['name'],
                "queue_cnt" => $syncCnt
            );
            $this->load->view("channel_product/sync_product", $data);
        }
    }

    /*
        提交同步商品请求
    */
    public function commitSyncProduct()
    {
        $chn_id = $_GET['chn_id'];
        $store_id = $_GET['store_id'];
        if(IS_POST){
            $this->load->model("ChannelProduct_model", "ChannelProductModel");
            $ChannelProductModel = $this->ChannelProductModel;

            $res = $ChannelProductModel->commitSyncProduct($chn_id, $store_id);

            header('Content-type: application/json');
            echo json_encode($res);
        }
    }

    /*
        同步第三方库存信息
    */
    public function syncStock()
    {
        $store_id = $_GET['store_id'];

        $this->load->model("ChannelProduct_model", "ChannelProductModel");
        $ChannelProductModel = $this->ChannelProductModel;

        if(IS_POST){
            

            $productList = $ChannelProductModel->getProductStock($store_id);

            $res = array(
                "status" => "success",
                "data" => $productList
            );
            header('Content-type: application/json');
            echo json_encode($res);
        }else{
            $this->load->model("Common_model", "CommonModel");
            $CommonModel = $this->CommonModel;

            $channel = $CommonModel->getAllChannelInfo($chn_id);
            $queueCnt = $ChannelProductModel->getSyncStockQueueCnt();

            foreach ($channel as $key => $value) {
                $channel[$key]['sync_cnt'] = $queueCnt[$key];
            }

            $data = array(
                "channel" => $channel
            );
            $this->load->view("/channel_product/sync_stock", $data);
        }
    }

    /*
        提交同步第三方库存请求
    */
    public function commitSyncStock()
    {
        $store_id = $_GET['store_id'];
        if(IS_POST){
            $this->load->model("ChannelProduct_model", "ChannelProductModel");
            $ChannelProductModel = $this->ChannelProductModel;

            $res = $ChannelProductModel->commitSyncStock($store_id);

            header('Content-type: application/json');
            echo json_encode($res);
        }
    }

    /*
        同步商品价格
    */
    public function syncPrice()
    {
        if(IS_POST){
            $chn_id = explode("|", trim($_POST['chn_id']));
            $store_id = explode("|", trim($_POST['store_id']));
            $product_price = explode("|", trim($_POST['product_price']));

            $priceInfo = array();
            foreach ($product_price as $key => $value) {
                $tmpInfo = explode(",", $value);

                $tmpId = intval($tmpInfo[0]);
                $tmpPrice = floatval($tmpInfo[1]);

                if($tmpId > 0 && $tmpPrice > 0){
                    $priceInfo[$tmpId] = $tmpPrice * 100;
                }

            }

            if(empty($chn_id) || empty($store_id) || empty($priceInfo)){
                $res = array(
                    "status" => "error",
                    "info" => "错误的数据"
                );

                header('Content-type: application/json');
                echo json_encode($res);exit;
            }

            $this->load->model("ChannelProduct_model", "ChannelProductModel");
            $ChannelProductModel = $this->ChannelProductModel;

            $priceList = $ChannelProductModel->getSyncPriceList($chn_id, $store_id, $priceInfo);

            $res = array(
                "status" => "success",
                "data" => $priceList
            );

            header('Content-type: application/json');
            echo json_encode($res);exit;
        }
    }

    /*
        提交同步商品价格
    */
    public function commitSyncPrice()
    {
        if(IS_POST){
            $chn_id = explode("|", trim($_POST['chn_id']));
            $store_id = explode("|", trim($_POST['store_id']));
            $product_price = explode("|", trim($_POST['product_price']));

            $priceInfo = array();
            foreach ($product_price as $key => $value) {
                $tmpInfo = explode(",", $value);

                $tmpId = intval($tmpInfo[0]);
                $tmpPrice = floatval($tmpInfo[1]);

                if($tmpId > 0 && $tmpPrice > 0){
                    $priceInfo[$tmpId] = $tmpPrice * 100;
                }

            }

            if(empty($chn_id) || empty($store_id) || empty($priceInfo)){
                $res = array(
                    "status" => "error",
                    "info" => "错误的数据"
                );

                header('Content-type: application/json');
                echo json_encode($res);exit;
            }

            $this->load->model("ChannelProduct_model", "ChannelProductModel");
            $ChannelProductModel = $this->ChannelProductModel;

            $ChannelProductModel->commitSyncPriceList($chn_id, $store_id, $priceInfo);

            $res = array(
                "status" => "success"
            );

            header('Content-type: application/json');
            echo json_encode($res);exit;
        }
    }

    /*
        获取城市列表
    */
    public function getCityList()
    {
        if(IS_POST){
            $this->load->model("Common_model", "CommonModel");
            $CommonModel = $this->CommonModel;

            $cityInfo = $CommonModel->getCityList();

            $res = array(
                "status" => "success",
                "data" => $cityInfo
            );

            header('Content-type: application/json');
            echo json_encode($res);
        }else{
            $this->load->view("/channel_product/sync_city", $data);
        }
    }

    /*
        获取药店列表
    */
    public function getStoreList()
    {
        $city_id = $_GET['city_id'];
        $this->load->model("Common_model", "CommonModel");
        $this->load->model("Store_model", "StoreModel");
        $this->load->model("ChannelProduct_model", "ChannelProductModel");
        
        $CommonModel = $this->CommonModel;
        $StoreModel = $this->StoreModel;
        $ChannelProductModel = $this->ChannelProductModel;

        if(IS_POST){
            $storeList = $StoreModel->getStoreListByCityId($city_id);

            $res = array(
                "status" => "success",
                "data" => $storeList
            );
            header('Content-type: application/json');
            echo json_encode($res);
        }else{
            $channel = $CommonModel->getAllChannelInfo();
            $queueCnt = $ChannelProductModel->getSyncPriceQueueCnt();

            foreach ($channel as $key => $value) {
                $channel[$key]['sync_cnt'] = $queueCnt[$key];
            }

            $cityInfo = $CommonModel->getCityList($city_id);
            $data = array(
                "city_name" => $cityInfo[$city_id]['Caption'],
                "channel" => $channel
            );
            $this->load->view("/channel_product/sync_store", $data);
        }
    }

    /*
        获取渠道列表
    */
    public function getChnList()
    {
        if(IS_POST){
            $this->load->model("Common_model", "CommonModel");
            $CommonModel = $this->CommonModel;
            $channel = $CommonModel->getAllChannelInfo();

            $res = array(
                "status" => "success",
                "data" => $channel
            );
            header('Content-type: application/json');
            echo json_encode($res);
        }
    }
}